<template>
  <div>
    <!--面包屑-->
    <el-breadcrumb separator="/" style="padding-left: 10px;padding-bottom: 10px;font-size: 12px">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--用户列表卡片-->
    <el-card class="box-card">
      <el-form :inline="true" :model="userVo" class="demo-form-inline">
        <el-form-item label="部门" label-width="70px">
          <el-select v-model="userVo.departmentId" placeholder="请选择" clearable>
            <el-option
              v-for="item in departments"
              :key="item.id"
              :label="item.name"
              :value="item.id">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">
              <span class="el-tag el-tag--success el-tag--mini el-tag--plain">{{item.depCount}}人</span>
            </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="70px">
          <el-input clearable v-model="userVo.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" label-width="70px">
          <el-input clearable v-model="userVo.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item>
          <el-radio v-model="userVo.sex" label="0">帅哥</el-radio>
          <el-radio v-model="userVo.sex" label="1">美女</el-radio>
          <el-radio v-model="userVo.sex" label="">全部</el-radio>
        </el-form-item>
        <el-form-item label="昵称" label-width="70px">
          <el-input clearable v-model="userVo.nickname" placeholder="请输入昵称"></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 10px">
          <el-button icon="el-icon-refresh" @click="resetUserVo">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="query">查询</el-button>
          <el-button type="success" icon="el-icon-plus" @click="Show">添加</el-button>
          <el-button type="warning" icon="el-icon-download" @click="exportExcel">导出</el-button>
        </el-form-item>
      </el-form>
      <!--表格内容显示区域-->
      <el-table
        v-loading="loading"
        :data="userList"
        border
        max-height="400"
        style="width: 100%;height: 400px">
        <el-table-column
          prop="id"
          label="#"
          width="50">
        </el-table-column>
        <el-table-column
          prop="username"
          label="用户名"
          width="110">
        </el-table-column>
        <el-table-column
          prop="sex"
          label="性别"
          width="110">
          <template slot-scope="scope">
            <span class="el-tag el-tag--success el-tag--mini el-tag--plain">{{scope.row.sex==0?'帅哥':(scope.row.sex==1?'美女':'保密')}}</span>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          prop="departmentName"
          label="所属部门"
          width="180">
        </el-table-column>
        <!--sortable是添加一个排序-->
        <el-table-column
          sortable
          prop="birth"
          label="生日"
          width="180">
        </el-table-column>
        <el-table-column
          sortable
          prop="email"
          label="邮箱"
          width="180">
        </el-table-column>
        <el-table-column
          sortable
          prop="phoneNumber"
          label="电话"
          width="180">
        </el-table-column>

        <el-table-column
          prop="status"
          label="是否禁用"
          width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status==0"
              active-color="#ff4949"
              inactive-color="#13ce66"
              @change="alterState(scope.row)">
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column
          label="操作">
          <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="compileUser(scope.row.id)"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="del(scope.row.id)"></el-button>
          <el-button type="warning" size="mini" icon="el-icon-s-tools"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="padding-top: 15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"> <!--total总条数 pageSize每页显示的条数 current当前页码-->
      </el-pagination>

      <!-- 引用userAdd -->
      <userAdd :addOrUpdateVisible="addOrUpdateVisible" :getUserList="getUserList" :userId="userId" @changeShow="showAddOrUpdate" ref="addOrUpdateRef"></userAdd>
    </el-card>
  </div>
</template>

<script>
    import {del, findUserList, editUserState, exportUserInfo} from '../../api/users'
    import {findDeptAndCount} from '../../api/department'
    import userAdd from '../../components/system/UserAdd'

    export default {
        name: 'Users',
        data() {
            return {
                addOrUpdateVisible: false,
                userVo: {
                    departmentId: '',
                    username: '',
                    email: '',
                    sex: '',
                    nickname: '',
                },
                loading: true,
                /*用户集合*/
                userList: [],
                //部门集合
                deptList: [],
                /*总条数*/
                total: 0,
                /*每页条数*/
                size: 5,
                /*当前页码*/
                current: 1,
                departments: [],
                userId: null
            }
        },
        //引用组件
        components: {
            userAdd
        },
        /*插件组件的时候调用查询所有用户的方法*/
        created() {
            this.getUserList()
            this.getDeptAndCount()
        },
        methods: {
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.size = val;
                this.getUserList();
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.current = val;
                this.getUserList();
            },
            /*查询用户信息*/
            async getUserList() {
                const {data} = await findUserList(this.current, this.size, this.userVo);
                this.userList = data.data.userList.records;
                this.total = data.data.userList.total;
                this.loading = false;
            },
            /*查询部门*/
            async getDeptAndCount() {
                const {data} = await findDeptAndCount()
                this.departments = data.data.departments
                console.log(data.data.departments)
            },
            /*重置表单*/
            resetUserVo() {
                this.userVo = {};
                this.getUserList()
            },
            // 按钮点击事件 显示新增编辑弹窗组件
            Show() {
                this.addOrUpdateVisible = true
            },
            // 监听 子组件弹窗关闭后触发，有子组件调用
            showAddOrUpdate(data) {
                if (data === 'false') {
                    this.addOrUpdateVisible = false
                    this.userId = null
                } else {
                    this.addOrUpdateVisible = true
                }
                this.getUserList()
            },
            // 删除
            async del(id) {
                var res = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                    .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
                if(res == "confirm"){
                    const { data: res } = await del(id);
                    // console.log(res);
                    if(res.code == 200){
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getUserList();
                        this.getDeptAndCount();
                    }
                }else{
                    this.$message.error(res.msg);
                }
            },
            //编辑
            compileUser(id){
                this.userId = id
                this.addOrUpdateVisible = true
            },
            // 禁用
            async alterState(row){
                const {data} = await editUserState(row.id)
                if(data.code==200){
                    var message=row.status?'用户状态已禁用':'用户状态已启用';
                    this.$notify({
                        title: '成功',
                        message: message,
                        type: 'success'
                    });
                }
                this.getUserList()

            },
            // 导出
            async exportExcel(){
                exportUserInfo(this.queryMap.roleName).then(res => {
                    //这里没有返回数据,但是要在响应头中进行下载操作
                    // 将二进制文件转化为可访问的url
                    let url = window.URL.createObjectURL(new Blob([res.data]));

                    //创建了一个a标签的超链接
                    var a = document.createElement("a");
                    //将a标签添加到body内容中
                    document.body.appendChild(a);
                    a.href = url;
                    a.download = "用户列表.xlsx";
                    a.click();
                    window.URL.revokeObjectURL(url);
                }).catch(err => {

                })
            },
            query(){
                this.current=1;
                this.getUserList()
            }
        }
    }
</script>

<style scoped>

</style>
